<?php
class visitchController extends AController {
	
	// 住院回访
	public function indexAction() {
		$page = $this->request->getGetParameter('p');
		$pageSize = 10;
		if (!$page) $page = 1;
	
		// 搜索条件
		$startTime = $this->request->getGetParameter('startTime');
		$endTime = $this->request->getGetParameter('endTime');
		// 获取本医院开通的回访规则中本规则的相应查询参数，本处需要规定产科在该医院中的相应编号实际上在url中进行获取
		$selDep = $this->request->getGetParameter('depts');
		$this->selDep = $selDep; // 自动从后台数据库中搜索产科，妇科，妇产科的科室
		$selDep = explode(',', $selDep);
		ATools::addSquote($selDep);
		$selStatus = $this->request->getGetParameter('selStatus');
		ATools::addSquote($selStatus);
		$doc = $this->request->getGetParameter('doc');
		$where = '1=1'.($startTime ? " AND ZY.CY_DATE>='{$startTime}'" : '')
				.($endTime ? " AND ZY.CY_DATE<='{$endTime}'" : '')
				.($doc ? " AND ZY.ZYDOC LIKE '%{$doc}%'" : '')
				.(count($selDep) > 0 ? " AND ZY.CY_DEP_NO IN (".implode(',', $selDep).")" : '')
				.(count($selStatus) > 0 ? 
					(in_array("'A'", $selStatus)?  " AND (V.VISIT_STATE IN (".implode(',', $selStatus).") OR 
							V.VISIT_STATE IS NULL)" : (" AND V.VISIT_STATE IN (".implode(',', $selStatus).")")) : '');
		
		$fields = "P.PATIENT_ID, P.TRUE_NAME, P.PHONE, P.SEX, P.BIRTH, ZY.RY_DATE, ZY.CY_DATE, ZY.CY_DEP, ZY.ZYDOC, ZY_AGE, ZY.BIZ_CODE,
		CASE 
  WHEN COUNT(1) > 1 THEN to_char(COUNT(1))
  ELSE V.VISIT_STATE
END AS VISIT_STATE,
max(V.VISIT_TIME) as LAST_VISIT_TIME,
V.VISIT_STATE AS LAST_VISIT_STATE";
		
		$sql = "SELECT {$fields} FROM ZHUYUAN ZY 
				LEFT JOIN PATIENT P ON ZY.REF_PATIENT_ID=P.PATIENT_ID
				LEFT JOIN VISIT V ON 'ZY_'||ZY.ZY_NO||'_'||ZY_TIMES=V.BIZ_CODE
				WHERE {$where}
				GROUP BY P.PATIENT_ID, P.TRUE_NAME, P.PHONE, P.BIRTH, P.SEX, ZY.RY_DATE, ZY.CY_DATE, ZY.CY_DEP, ZY.ZYDOC, ZY_AGE, ZY.BIZ_CODE, ZY.ZY_CODE,VISIT_STATE
				ORDER BY ZY.ZY_CODE";

		// TODO 暂时采用本方法获取下一位信息
		$visitSql['where'] = $where;
		$visitSql['fields'] = $fields;
		$visitSql['orderField'] = 'ZY.ZY_CODE';
		$visitSql['orderType'] = 'ASC';
		$_SESSION['visitZySql'] = $visitSql;
		
		$params = $this->request->getGetParameters();
		unset($params['p']);
		unset($params['c']);
		unset($params['a']);
		
		$totalCount = 0;
		$this->startNum = ($page - 1) * $pageSize + 1;
		$this->rows = JYDB::query($sql, $page, $pageSize, $totalCount);
		$this->pager = JYTools::pager('index.php?c=visitch&a=index&p=_num_&'.http_build_query($params), $totalCount, $page);
		$this->startTime = $startTime;
		$this->endTime = $endTime;
		$this->depts = JYDep::getZyDepts(CSM_UNIT_ID);
		$this->selStatus = $selStatus;
		return $this->render();
	}
	
	// 住院回访个人信息
	public function entryAction() {
		$visitSql = $_SESSION['visitZySql'];
		$fields = $visitSql['fields'];
		$where = $visitSql['where'];
		$page = $this->request->getGetParameter('p');
		$this->next = $page + 1;
		$sql = "SELECT {$fields} FROM ZHUYUAN ZY
		LEFT JOIN PATIENT P ON ZY.REF_PATIENT_ID=P.PATIENT_ID
		LEFT JOIN VISIT V ON 'ZY_'||ZY.ZY_NO||'_'||ZY_TIMES=V.BIZ_CODE
		WHERE {$where}
		GROUP BY P.PATIENT_ID, P.TRUE_NAME, P.PHONE, P.BIRTH, P.SEX, ZY.RY_DATE, ZY.CY_DATE, ZY.CY_DEP, ZY.ZYDOC, ZY_AGE, ZY.BIZ_CODE, ZY.ZY_CODE,VISIT_STATE
		ORDER BY ZY.ZY_CODE";
		
		$row = JYDB::query($sql, $page, 1);
		if (!$row) {
			return $this->alert('对不起，您现在已经回访到最后一位客户了！');
		}
		
		
		$pid = $row[0]['PATIENT_ID'];
		
		// 获取病人基本资料
		$this->pid = $pid;
		$this->patient = JYDB::find('PATIENT', "PATIENT_ID={$pid}");

		// 获取病人病人的最近住院信息
		$zhuyuan = JYDB::find('ZHUYUAN', "REF_PATIENT_ID={$pid}", '*', 'CY_DATE DESC');
		
		// 病人最近的手术信息
		if ($zhuyuan['IS_OPERATE']) {
			$this->operation = JYDB::find('OPERATION', "ZY_NO='{$zhuyuan['ZY_NO']}' AND ZY_TIMES='{$zhuyuan['ZY_TIMES']}'", '*', 'OP_TIMES DESC');
		}
		
		$this->zhuyuan = $zhuyuan;
		return $this->render();
	}
	
	// 回访保存
	public function resultAction() {
		// 获取参数
		$status = $this->request->getPostParameter('status');
		$result = '';
		if ($status) {
			$result = $this->request->getPostParameter('result');
		}
		$content = $this->request->getPostParameter('content');
		$track = $this->request->getPostParameter('track', '0');
		$visitType = $this->request->getPostParameter('type');
		$rule = $this->request->getPostParameter('rule');
		$bizCode = $this->request->getPostParameter('bizcode');
		$sysId = $this->request->getPostParameter('sysid');
		$visitCode = CSM_UNIT_ID.'_'.CSM_USER_ID.'_'.$bizCode;
		$visitTime = date('Y-m-d h:i:s');
		$patientId = $this->request->getPostParameter('pid');
		
		$row = array('VISIT_CODE'=>$visitCode,
				'VISIT_TIME'=>$visitTime,
				'VISIT_TYPE'=>$visitType,
				'USER_ID'=>CSM_USER_ID,
				'USER_NAME'=>CSM_USER_NAME,
				'UNIT_ID'=>CSM_UNIT_ID,
				'USER_NO'=>$_SESSION['user']['DOC_NO'],
				'VISITRULE_ID'=>$rule,
				'VISIT_RESULT'=>$result,
				'VISIT_STATE'=>$status,
				'CONTENT'=>$content,
				'IS_THANK'=>'0',
				'IS_TRACK'=>$track,
				'SYS_ID'=>$sysId,
				'BIZ_CODE'=>$bizCode);
		
		$rs = JYDB::create('VISIT', $row, 'ID');
		$trackResult = true;
		if ($rs and $track == '1') {
			$trackRow = array('VISIT_CODE'=>$visitCode,
					'PATIENT_ID'=>$patientId,
					'CREATE_TIME'=>$visitTime,
					'CREATE_USER'=>CSM_USER_NAME,
					'CONTENT'=>$content,
					'STATE'=>'0',
					'UNIT_ID'=>CSM_UNIT_ID,
					'DEP_NO'=>'',
					'DOC_NO'=>'');
			$trackResult = JYDB::create('VISIT_TRACK', $trackRow, 'T_ID');
		}
		if (!$rs or !$trackResult) {
			return json_encode(array('code'=>'0', 'msg'=>'回访结果保存失败'));
		} else {
			return json_encode(array('code'=>'1', 'msg'=>'回访结果保存成功'));
		}
	}
	
}